<?php
class Member_connectModel extends Model {
    public function _initialize(){
        parent::_initialize();
    }
    public function getAll($id,$levelid){//所有的两级下级（可指定第二级）
        $mybelong=$this->where('member_id = '.$id)->getfield('referee_id',true);
        foreach($mybelong as $k =>$v){
            $mysecond=$this->where('member_id = '.$v)->getfield('referee_id',true);
            foreach($mysecond as $key => $val){
                array_push($mybelong,$val);
            }
        }
        //$mybelong 一维数组 ，存储自己的下级以及下级的下级（共两级）
        foreach($mybelong as $k =>$v){
            $level=D('Member')->where('id = '.$v)->getfield('level');
            if(!empty($levelid)){
                if($level != $levelid){
                    unset($mybelong[$k]);
                }
            }
        }
        return($mybelong);
    }
    public function getAll2($id,$levelid,$levelid2){//所有的两级下级（可指定一级和二级的身份）
        $mybelong=$this->where('member_id = '.$id.' and type = '.$levelid)->getfield('referee_id',true);
        $secbelong=array();
        foreach($mybelong as $k =>$v){
            $mysecond=$this->where('member_id = '.$v.' and type = '.$levelid2)->getfield('referee_id');
            array_push($secbelong,$mysecond);
        }
        return(array_filter($secbelong));
    }

    public function mybelong($id,$levelid){
        $mybelong = $this->where('member_id = ' . $id)->getfield('referee_id', true);
        foreach ($mybelong as $k => $v) {
            $level=D('Member')->where('id = '.$v)->getfield('level');
            if(!empty($levelid)){
                if($level != $levelid){
                    unset($mybelong[$k]);
                }
            }
        }
        return($mybelong);
    }
    public function secSeller($id){//业务员商家
        return $this->getAll2($id,5,1);
    }
    public function serverSeller($id){//服务商商家
        return $this->getAll2($id,6,1);
    }
    public function getSeller($id){//商家
        return $this->getAll($id,1);
    }
    public function getServer($id){//服务商
        return $this->getAll($id,6);
    }
    public function mySeller($id){//直属商家
        return $this->mybelong($id,1);
    }
    public function myServer($id){//直属服务商
        return $this->mybelong($id,6);
    }
    public function totalCash($id){//总收益（当前用户（服务商））
        $memberlist=$this->mybelong($id,1);
        $sum=0;
        foreach($memberlist as $k => $v){
            $sum+=D('Member_account_log')->where('member_id = '.$id.' and from_member = '.$v)->sum('changenum');
        };
        return $sum;
    }

    public function deleteconnect($member,$referee){
        $id=$this->where('member_id = '.$member.' and referee_id = '.$referee)->getfield('id');
        if($id){
            $re=$this->where('id = '.$id)->delete();
            if($re){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
}